package com.ghost.Chessboard;



public class Line {
    
    private int startfrom;
    private final int startturnstate;// 1:downward 2:upward 3: rightward 4:
				     // leftward

    private int nowturnstate;
    private int startx;
    private int starty;
    private int nowx;
    private int nowy;
    private  int seecount = 0;
    private  boolean state = true; // true eye,false mirror

    public Line(int startfrom, int startturnstate, int x, int y) {
	// TODO Auto-generated constructor stub
	this.startfrom = startfrom;
	this.startturnstate = startturnstate;
	this.nowturnstate = startturnstate;
	this.startx = x;
	this.starty = y;
	this.nowx=x;
	this.nowy=y;

    }

    public int getseecount() {
	// TODO Auto-generated method stub
	return seecount;
    }

    public int run(Chessboard obj) {
	// TODO Auto-generated method stub
//	 Pref.sysprint("###this.nowx: "+this.nowx);
//	 Pref.sysprint("###this.nowy: "+this.nowy);
	 
	while(this.nowx>=0 && this.nowx<obj.getWidthnum() && this.nowy>=0 && this.nowy<obj.getHeightnum()) {
	int x = this.nowx;
	int y = this.nowy;
	// Pref.sysprint("\n###SSS2: "+this.seecount);

	if (obj.chessmap[y][x].id == 0) {

	} else if (obj.chessmap[y][x].id == 1) {  //mirror_toleft
	     this.mirrorturn(1);
	     this.state=false;
	     Pref.sysprint("\n#@@@@@mirror1@##xy: "+this.nowx+":"+this.nowy);
	} else if (obj.chessmap[y][x].id == 2) {    //mirror_toright
	     this.mirrorturn(2);
	     this.state=false;
	     Pref.sysprint("\n#@@@@@@@mirror2@@@##xy: "+this.nowx+":"+this.nowy);
	} else if (obj.chessmap[y][x].seebyeye && this.state) {
	    this.seecount += 1;
	    Pref.sysprint("\n#@@@@@@@@@@@@@@@@@##xy: "+this.nowx+":"+this.nowy);
	    Pref.sysprint("\n#@@@@@@@@@@@@@@@@@##seecountnow: "+this.seecount);
	     
	} else if (obj.chessmap[y][x].seebymirror && !this.state) {
	    this.seecount += 1;
	    Pref.sysprint("\n#@@@@@@@@@@@@@@@@@##xy: "+this.nowx+":"+this.nowy);
	    Pref.sysprint("\n##@@@@@@@@@@@@@@@@@@@@#seecountnow: "+this.seecount);
	     
	}
	this.go();
	
	Pref.sysprint("\n#@@@^^^^^^^^^^@@##xy: "+this.nowx+":"+this.nowy);
	}
	return this.seecount;
    }

    private void go() {
	// 1:downward 2:upward 3: rightward 4:  leftward
	switch (this.nowturnstate) {
	case Pref.DOWNWARD: {
	    this.nowy+=1;
	    break;
	    
	}
	case Pref.UPWARD: {
	    this.nowy-=1;
	    break;

	}
	case Pref.RIGHTWARD: {
	    this.nowx+=1;
	    break;

	}
	case Pref.LEFTWARD: {
	    this.nowx-=1;
	    break;

	}
	}

    }
    
    private void mirrorturn(int mirrorto) {
	// 1:downward 2:upward 3: rightward 4:  leftward
	switch (this.nowturnstate) {
	case Pref.DOWNWARD: {
	    this.nowturnstate= (mirrorto == 1 ? Pref.LEFTWARD : Pref.RIGHTWARD);
	    break;
	}
	case Pref.UPWARD: {
	    this.nowturnstate= (mirrorto == 1 ? Pref.RIGHTWARD: Pref.LEFTWARD);
	    break;
	}
	case Pref.RIGHTWARD: {
	     
	    this.nowturnstate= (mirrorto == 1 ? Pref.UPWARD: Pref.DOWNWARD);
	    break;
	}
	case Pref.LEFTWARD: {
	     
	    this.nowturnstate= (mirrorto == 1 ? Pref.DOWNWARD : Pref.UPWARD);
	    break;
	}
	}

    }

}
